/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package view.jPanels;

import com.mysql.jdbc.MysqlDataTruncation;
import java.sql.SQLException;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import javax.swing.JDialog;
import javax.swing.JFrame;
import static javax.swing.JFrame.EXIT_ON_CLOSE;
import javax.swing.JOptionPane;
import javax.swing.JTabbedPane;
import javax.swing.JTextField;
import regraDeNegocio.RNNotaFiscal;
import view.Inicio;

/**
 *
 * @author Gean
 */
public class CadNotaFiscal extends javax.swing.JPanel {

    /**
     * Creates new form CadNotaFiscal
     */
    private ArrayList<JTextField> camposTextoObrig;
    JTabbedPane pane;
    Inicio inicio;
    
    public CadNotaFiscal(JTabbedPane pane, Inicio inicio) {
        initComponents();
        
        this.pane = pane;
        this.inicio = inicio;
        camposTextoObrig = new ArrayList<>();
        
        camposTextoObrig.add(jTextFieldDtEmissao);
        camposTextoObrig.add(jTextFieldEmpenho);
        camposTextoObrig.add(jTextFieldFornec);
        camposTextoObrig.add(jTextFieldNum);
        camposTextoObrig.add(jTextFieldValor);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jSeparator2 = new javax.swing.JSeparator();
        jLabel32 = new javax.swing.JLabel();
        jLabel37 = new javax.swing.JLabel();
        jLabel33 = new javax.swing.JLabel();
        jTextFieldDtEmissao = new javax.swing.JTextField();
        jButtonConcluir = new javax.swing.JButton();
        jLabel35 = new javax.swing.JLabel();
        jTextFieldFornec = new javax.swing.JTextField();
        jLabel36 = new javax.swing.JLabel();
        jTextFieldValor = new javax.swing.JTextField();
        jTextFieldNum = new javax.swing.JTextField();
        jLabel34 = new javax.swing.JLabel();
        jTextFieldEmpenho = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jSeparator1 = new javax.swing.JSeparator();
        jSeparator3 = new javax.swing.JSeparator();
        jButtonPesqFornec = new javax.swing.JButton();
        jButton1 = new javax.swing.JButton();

        setBorder(javax.swing.BorderFactory.createEtchedBorder(javax.swing.border.EtchedBorder.RAISED, new java.awt.Color(51, 51, 51), null));
        setToolTipText("Formulário de Cadastro de Nota Fiscal");

        jLabel32.setFont(new java.awt.Font("Tahoma", 1, 16)); // NOI18N
        jLabel32.setText("Cadastro de Nota Fiscal");

        jLabel37.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jLabel37.setText("Valor Total (em Real):");

        jLabel33.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jLabel33.setText("Número:");

        jTextFieldDtEmissao.setToolTipText("Digite a Data de Emissão da Nota Fiscal");
        jTextFieldDtEmissao.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTextFieldDtEmissaoActionPerformed(evt);
            }
        });

        jButtonConcluir.setText("Concluir");
        jButtonConcluir.setToolTipText("Concluir Cadastro");
        jButtonConcluir.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonConcluirActionPerformed(evt);
            }
        });

        jLabel35.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jLabel35.setText("Fornecedor:");

        jTextFieldFornec.setEditable(false);
        jTextFieldFornec.setToolTipText("Digite o Fornecedor da Nota Fiscal");
        jTextFieldFornec.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTextFieldFornecActionPerformed(evt);
            }
        });

        jLabel36.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jLabel36.setText("Data de Emissão:");

        jTextFieldValor.setToolTipText("Digite o Valor da Nota Fiscal");

        jTextFieldNum.setToolTipText("Digite o Número da Nota Fiscal");

        jLabel34.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jLabel34.setText("Empenho:");

        jTextFieldEmpenho.setToolTipText("Digite o Empenho da Nota Fiscal");

        jLabel1.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jLabel1.setForeground(new java.awt.Color(255, 0, 0));
        jLabel1.setText("Todos os campos são obrigatórios *");

        jLabel2.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jLabel2.setForeground(new java.awt.Color(255, 0, 0));
        jLabel2.setText("*");

        jLabel3.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jLabel3.setForeground(new java.awt.Color(255, 0, 0));
        jLabel3.setText("*");

        jLabel4.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jLabel4.setForeground(new java.awt.Color(255, 0, 0));
        jLabel4.setText("*");

        jLabel5.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jLabel5.setForeground(new java.awt.Color(255, 0, 0));
        jLabel5.setText("*");

        jLabel6.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jLabel6.setForeground(new java.awt.Color(255, 0, 0));
        jLabel6.setText("*");

        jButtonPesqFornec.setIcon(new javax.swing.ImageIcon("C:\\Users\\Gean\\Desktop\\rp-prototipo\\src\\view\\imagens\\lupa.png")); // NOI18N
        jButtonPesqFornec.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonPesqFornecActionPerformed(evt);
            }
        });

        jButton1.setIcon(new javax.swing.ImageIcon("C:\\Users\\Gean\\Desktop\\RP_Ultimo\\rp-prototipo\\src\\view\\imagens\\fechar.png")); // NOI18N
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        try{ 
            javax.swing.text.MaskFormatter dt = new javax.swing.text.MaskFormatter("####/##/##"); 
            jTextFieldDtEmissao = new javax.swing.JFormattedTextField(dt); 
        } 
        catch (Exception e){ 
            System.out.println("erro mascara");
        }
        jTextFieldValor.setDocument(new LimitarCaracteres(12));
        jTextFieldNum.setDocument(new LimitarCaracteres(45));
        jTextFieldEmpenho.setDocument(new LimitarCaracteres(45));

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel32)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel2)
                                    .addComponent(jLabel3)
                                    .addComponent(jLabel4)
                                    .addComponent(jLabel5)
                                    .addComponent(jLabel6))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel33)
                                    .addComponent(jLabel34)
                                    .addComponent(jLabel35)
                                    .addComponent(jLabel36)
                                    .addComponent(jLabel37))
                                .addGap(18, 18, 18)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(layout.createSequentialGroup()
                                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                            .addComponent(jTextFieldDtEmissao, javax.swing.GroupLayout.DEFAULT_SIZE, 400, Short.MAX_VALUE)
                                            .addComponent(jTextFieldValor, javax.swing.GroupLayout.DEFAULT_SIZE, 400, Short.MAX_VALUE)
                                            .addComponent(jTextFieldFornec)
                                            .addComponent(jTextFieldEmpenho))
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(jButtonPesqFornec, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE))
                                    .addComponent(jTextFieldNum, javax.swing.GroupLayout.PREFERRED_SIZE, 400, javax.swing.GroupLayout.PREFERRED_SIZE)))
                            .addComponent(jLabel1))
                        .addContainerGap(376, Short.MAX_VALUE))))
            .addComponent(jSeparator1)
            .addComponent(jSeparator3)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jButtonConcluir, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(22, 22, 22))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jLabel32))
                    .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 24, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel33)
                    .addComponent(jTextFieldNum, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel34)
                    .addComponent(jTextFieldEmpenho, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel3))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel35)
                        .addComponent(jTextFieldFornec, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jLabel4))
                    .addComponent(jButtonPesqFornec))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel36)
                    .addComponent(jTextFieldDtEmissao, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel5))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel37)
                    .addComponent(jTextFieldValor, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel6))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jSeparator3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jButtonConcluir, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void jButtonConcluirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonConcluirActionPerformed
        try{
            int cont=0;
             for (int i = 0; i < camposTextoObrig.size(); i++) {
                if(camposTextoObrig.get(i).getText().equals("")){
                cont++;
                 }
              }    
                if(cont>0){
                    JOptionPane.showMessageDialog(this, "TODOS OS CAMPOS OBRIGATÓRIOS DEVEM SER PREENCHIDOS !");    
                }else{
                 int testa = Integer.parseInt(jTextFieldNum.getText());
                 int testa2 = Integer.parseInt(jTextFieldEmpenho.getText());
                 int test3 = Integer.parseInt(jTextFieldValor.getText());
                    
                 String armazena = jTextFieldDtEmissao.getText().toString().replaceAll("/", "-");
                  
                 RNNotaFiscal rnNotaFiscal = new RNNotaFiscal(jTextFieldNum.getText(), jTextFieldEmpenho.getText(), 
                 armazena, Float.parseFloat(jTextFieldValor.getText()), jTextFieldFornec.getText());
                
                 rnNotaFiscal.insertBDNF(rnNotaFiscal);
                 
                 JOptionPane.showMessageDialog(this, "CADASTRO CONCLUÍDO COM SUCESSO !");
                 pane.remove(this);
                 inicio.setContadorNF(0);
                }
        
        }catch(NumberFormatException e){
            JOptionPane.showMessageDialog(this, "CAMPOS NÚMERICOS PREENCHIDOS INCORRETAMENTE !");
        }catch(MysqlDataTruncation e){
            JOptionPane.showMessageDialog(this, "A DATA INFORMADA É INVÁLIDA !");
        }catch(SQLException e){
            JOptionPane.showMessageDialog(this, "ESSA NOTA FISCAL JA ESTÁ CADASTRADA !");
         }  
        
    }//GEN-LAST:event_jButtonConcluirActionPerformed
   
    private void jButtonPesqFornecActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonPesqFornecActionPerformed
        JDialog novaJanelaFornec = new JDialog();
        
        SelecionarFornecedor fornecedor = new SelecionarFornecedor(this, novaJanelaFornec);
        fornecedor.setVisible(true);
        fornecedor.setEnabled(true);
        
        novaJanelaFornec.setContentPane(fornecedor);
        novaJanelaFornec.setSize(980, 400);
        novaJanelaFornec.setLocationByPlatform(true);
        novaJanelaFornec.setLocationRelativeTo(null);
        novaJanelaFornec.setModal(true);        
        novaJanelaFornec.setAlwaysOnTop(true);
        novaJanelaFornec.setVisible(true);
        
    }//GEN-LAST:event_jButtonPesqFornecActionPerformed
  
    private void jTextFieldFornecActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jTextFieldFornecActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jTextFieldFornecActionPerformed

    private void jTextFieldDtEmissaoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jTextFieldDtEmissaoActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jTextFieldDtEmissaoActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        pane.remove(this);
        inicio.setContadorNF(0);
    }//GEN-LAST:event_jButton1ActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButtonConcluir;
    private javax.swing.JButton jButtonPesqFornec;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel32;
    private javax.swing.JLabel jLabel33;
    private javax.swing.JLabel jLabel34;
    private javax.swing.JLabel jLabel35;
    private javax.swing.JLabel jLabel36;
    private javax.swing.JLabel jLabel37;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JSeparator jSeparator2;
    private javax.swing.JSeparator jSeparator3;
    private javax.swing.JTextField jTextFieldDtEmissao;
    private javax.swing.JTextField jTextFieldEmpenho;
    private javax.swing.JTextField jTextFieldFornec;
    private javax.swing.JTextField jTextFieldNum;
    private javax.swing.JTextField jTextFieldValor;
    // End of variables declaration//GEN-END:variables

    public JTextField getJTextFieldFornec(){
        return jTextFieldFornec;
    }
    
    public void setJTextFieldFornec(JTextField fornec){
        this.jTextFieldFornec = fornec;
    }
    
    
    public static void main(String[] args) {
             JFrame jf = new JFrame();
             jf.setVisible(true);
             jf.setSize(1024, 768);
             CadNotaFiscal nf = new CadNotaFiscal(null, null);
             jf.add(nf);
             jf.setDefaultCloseOperation(EXIT_ON_CLOSE);
             
    }

}
